package de.syfy.project.engine.hud;

import de.syfy.project.engine.interfaces.Game;
import de.syfy.project.engine.Texture;
import de.syfy.project.engine.TextureLoader;
import de.syfy.project.engine.hud.interfaces.UI_Container;
import de.syfy.project.engine.hud.interfaces.UI_Item;
import java.awt.geom.Point2D;
import java.io.IOException;
import static org.lwjgl.opengl.GL11.*;

/**
 * Button-Klasse dient zum Erstellen von rechteckigen Buttons mit benutzer-
 * definierten Texturen.
 * 
 * @author Timo
 */
public class Button extends UI_Item{
    
    private Texture standard,pressed,highlight,texture;
    private float width,height;
    private float x,y;
    private UI_Container parent;
    private EventListener listener;
    private Point2D.Float p;
    private int priority;
    
    public Button(Game g){
        super(g);
    }
    
    @Override
    public void setTexture(Texture texture) {
        this.texture = texture;
    }
    
    @Override
    public void setTextures(Texture standard, Texture pressed, Texture highlight){
        this.texture = standard;
        this.standard = standard;
        this.pressed = pressed;
        this.highlight = highlight;
    }
    
    @Override
    public void setTexture(TextureLoader loader, String path) {
        try {
            this.standard = loader.getTexture(path);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void setStandardTextures(Texture texture) {
        this.standard = texture;
        this.texture = texture;
    }

    @Override
    public void setStandardTextures(TextureLoader loader, String path, int x, int y, int width, int height) {
        try {
            this.standard = loader.getTexture(path, x, y, width, height);
            this.texture = standard;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void setPressedTextures(Texture texture) {
        this.pressed = texture;
    }

    @Override
    public void setPressedTextures(TextureLoader loader, String path, int x, int y, int width, int height) {
        try {
            this.pressed = loader.getTexture(path, x, y, width, height);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void setHighlightTextures(Texture texture) {
        this.highlight = texture;
    }

    @Override
    public void setHighlightTextures(TextureLoader loader, String path, int x, int y, int width, int height) {
        try {
            this.highlight = loader.getTexture(path, x, y, width, height);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public Texture getTexture() {
        return standard;
    }

    @Override
    public void setSize(float width,float height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public void setWidth(float width) {
        this.width = width;
    }

    @Override
    public void setHeight(float height) {
        this.height = height;
    }

    @Override
    public float getWidth() {
        return width;
    }

    @Override
    public float getHeight() {
        return height;
    }

    @Override
    public void setPosition(Point2D.Float p) {
        this.p = p;
    }

    @Override
    public Point2D.Float getPosition() {
        return p;
    }

    @Override
    public void setPriority(int priority) {
        this.priority = priority;
    }

    @Override
    public int getPriority() {
        return priority;
    }

    @Override
    public void itemClick(float x, float y) {
        if (isInside(x, y)) {
            if (listener != null) {
                listener.onMouseClick(new Event("Item-Click", new Point2D.Float(x, y), this));
            }
        }
    }

    @Override
    public void itemHover(float x, float y) {
        if (isInside(x, y)) {
            texture = highlight;
            if (listener != null) {
                listener.onMouseHover(new Event("Item-Hover", new Point2D.Float(x, y), this));
            }
        }else{
            texture = standard;
        }
    }
    
    private boolean isInside(float x, float y){
        return x >= this.x && x <= this.x + this.width && y >= this.y && y <= this.y + this.height;
    }

    @Override
    public void setParent(UI_Container parent) {
        this.parent = parent;
    }

    @Override
    public UI_Container getParent() {
        return parent;
    }

    @Override
    public void drawItem() {
        glEnable(GL_TEXTURE_2D); //Um zu gewährleisten dass Schriften später funzen
        glPushMatrix();
        texture.bind();
        glTranslatef(p.x, p.y, 0);
        glTranslatef(0,0,0.0f);
        glBegin(GL_QUADS);
            glTexCoord2f(0, 0);
            glVertex2f(0, 0);
            glTexCoord2f(0, texture.getHeight());
            glVertex2f(0, texture.getImageHeight());
            glTexCoord2f(texture.getWidth(), texture.getHeight());
            glVertex2f(texture.getImageWidth(), texture.getImageHeight());
            glTexCoord2f(texture.getWidth(), 0);
            glVertex2f(texture.getImageWidth(), 0);
        glEnd();
        glPopMatrix();
        glDisable(GL_TEXTURE_2D); //Um zu gewährleisten dass Schriften später funzen
    }

    @Override
    public int compareTo(Object o) {
        return -1;
    }

    @Override
    public void addEventListener(EventListener listener) {
        this.listener = listener;
    }
}
